<?php

namespace App\Console\Commands;

use App\Models\SalePlan;
use App\Models\SaleTask;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class TimeoutClose extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'timeoutclose:run';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // 查询三月份以前的计划
//        $end_time = strtotime('-65 days');
        $end_time = strtotime('2021-06-05 00:00:00');
        $maxId = SalePlan::where('end_time', '<=', $end_time)->whereNotNull('end_time')->orderByRaw('end_time - id desc')->value('id');
        // 授权状态 [1, 2, 3]
        $son = SaleTask::whereIn('status', [1, 2, 3])->whereRaw("plan_id in (select id from xx_sale_plan where id <= ?)", [$maxId]);
        // 审核状态 2
        $taskResult = SaleTask::distinct()->where('check_status', 2)->whereRaw("plan_id in (select id from xx_sale_plan where id <= ?)", [$maxId])
            ->union($son)->get();


        dd($taskResult->pluck('id'));
    }
}
